package com.github.tangmonkmeat;

/**
 * Description:
 *
 * @author zwl
 * @version 1.0
 * @date 2021/8/3 上午8:16
 */
public class 最短无序连续子数组 {

    public static void main(String[] args) {

        int len = findUnsortedSubarray(new int[]{3,0,4});

        System.out.println(len);

    }

    public static int findUnsortedSubarray(int[] nums) {
            int l = 0;
            int r = nums.length - 1;

            int pre_max = Integer.MIN_VALUE;
            int nx_min = Integer.MAX_VALUE;

            int len = nums.length;

            for(int i = 0;i < nums.length;i++){
                pre_max = Math.max(nums[i],pre_max);
                nx_min = Math.min(nums[len - 1 -i],nx_min);

                if(nums[i] < pre_max){
                    l = i;
                }
                if(nums[len - 1 - i] > nx_min){
                    r = len - 1 - i;
                }
            }

            return l > r ? l - r + 1 : 0;
    }
}
